package com.chixing.day17_reflect_thread.work;

public class PublicResource {
    private int number = 0;
    //资源生产
    public synchronized void produce(){
//		System.out.println("<><><><><><><>生产者线程抢到CPU<><><><><><><><><> number = " + number);
        if(number ==10){
            try {//不生产，生产者线程进入等待队列，等待消费者消费
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
            //执行生产，叫消费者来消费
            number++;
            System.out.println("【生产者】生产了一个面包，number = " + number);
            notify();//唤醒在等待队列的消费者线程来消费
        }
    }

    //资源消费
    public synchronized void consume(){
//		System.out.println("**********************消费者线程抢到CPU..................... number = " + number);
            if(number ==0){
                try {//不能消费，消费者线程进入等待队列，等待生产者生产
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                //执行消费，叫生产者来生产
                number--;
                System.out.println("【消费者】消费了一个面包，number = " + number);
                notify();//唤醒在等待状态的生产者来生产
            }
    }

    public static void main(String[] args) {
        PublicResource resource = new PublicResource();
        Thread thread1 = new Thread(new Producer(resource));
        Thread thread2 = new Thread(new Consumer(resource));
        thread1.start();
        thread2.start();
    }
}

